window.onload=function () {
    const E = window.wangEditor
    const editor = new E('#div1')
    const text = $('#text')
    // 监控变化，同步更新到 textarea
    editor.config.onchange = function (html) {
        text.val(html)
    }
    //设置上传的参数名
    editor.config.uploadFileName = 'file'
    // 配置 server 接口地址
    editor.config.uploadImgServer = 'upload-img.do';
    // 配置alt选项
    editor.config.showLinkImgAlt = false;
    // 配置超链接
    editor.config.showLinkImgHref = false;
    //去除全屏
    editor.config.showFullScreen = false;
    //上传时间
    editor.config.uploadImgTimeout = 20 * 1000;
    editor.config.placeholder = '请输入正文';
    editor.config.height = 500
    //配置菜单
    editor.config.menus = [
        'head',
        'bold',
        'fontSize',
        'fontName',
        'italic',
        'underline',
        'strikeThrough',
        'indent',
        'lineHeight',
        'foreColor',
        'backColor',
        'link',
        'list',
        'todo',
        'justify',
        'quote',
        'emoticon',
        'image',
        'code',
        'splitLine',
        'undo',
        'redo',
    ]
    editor.create();
    // 初始化 textarea 的值
    text.val(editor.txt.html());

    //动态获取帖子类型下拉框的选项
    // $.get('selectSelect.do',function (back) {
    //     $.each(back, function (index, value) {
    //         $('#selectType').append(new Option(value.ptype, value.tid));
    //     });
    // });

    //是否显示置顶
    $.get('selectUserRole.do',function (back) {
        if (back=="普通用户"){
            document.getElementById("topMes").style.display='none';
            document.getElementById("topCheck").style.display='none';
        }
    });

    //获取当前时间
    function getCurrentTime(){
        let now= new Date();
        let _month = ( 10 > (now.getMonth()+1) ) ? '0' + (now.getMonth()+1) : now.getMonth()+1;
        let _day = ( 10 > now.getDate() ) ? '0' + now.getDate() : now.getDate();
        let _hour = ( 10 > now.getHours() ) ? '0' + now.getHours() : now.getHours();
        let _minute = ( 10 > now.getMinutes() ) ? '0' + now.getMinutes() : now.getMinutes();
        let _second = ( 10 > now.getSeconds() ) ? '0' + now.getSeconds() : now.getSeconds();
        return now.getFullYear() + '-' + _month + '-' + _day + ' ' + _hour + ':' + _minute + ':' + _second;
    }
    //获取当日日期
    function getCurrentDate(){
        let now= new Date();
        let _month = ( 10 > (now.getMonth()+1) ) ? '0' + (now.getMonth()+1) : now.getMonth()+1;
        let _day = ( 10 > now.getDate() ) ? '0' + now.getDate() : now.getDate();
        return now.getFullYear() + '-' + _month + '-' + _day;
    }
    //标题/内容弹出提示
    function showtitlemsg(str){
        document.getElementById("titlemsg").innerHTML=str;
        setTimeout(hiden_titlemsg,1000);
    }
    function hiden_titlemsg(){
        document.getElementById("titlemsg").innerHTML="";
    }
    //判断输入框是否为空格
    function judgeTrim(str) {
        return str.replace(/(^\s*)|(\s*$)/g, "");
    }
    //弹窗
    // function showmsg(){
    //     document.getElementById("showmsg").style.display = 'block';
    //     setTimeout(hiden_search,1000);
    // }
    // function hiden_search(){
    //     document.getElementById("showmsg").style.display='none';
    // }
    //发表帖子
    $("#submit").click(function () {
        // var type = $("#selectType").val();
        var tid = $('#selectType option:selected').val();//选中的值
        var title = $("#title").val();
        var content = $("#text").val()
        var topcheck = document.getElementById("topCheck");
        if (topcheck.checked){
            pstatus=2
        }else{
            pstatus=1
        }
        var title2 = judgeTrim(title);
        var content2 = judgeTrim(content);
        if (title2==""){
            showtitlemsg("标题输入有误");
            return;
        }
        if (content2==""){
            showtitlemsg("内容有误");
        }else {
            var time = getCurrentTime();
            $.post('publishPost.do',{tid:tid,title:title,content:content,time:time,pstatus:pstatus,ldate:getCurrentDate()},function (back) {
                $("#title").val("");
                $("#text").val("");
                if(null!=back[0] || back[0]!=""){
                    // showmsg();
                    alert("发帖成功,加分为"+back[1]);
                    window.location.href='/html/editor/viewPost.do?pid='+back[0];
                }
            });
        }
    });
    //帖子存为草稿
    $("#draft").click(function () {
        var type = $("#selectType").val();
        var title = $("#title").val();
        var content = $("#text").val();
        var title2 = judgeTrim(title);
        var content2 = judgeTrim(content);
        if (title2==""){
            showtitlemsg("标题输入有误");
            return;
        }
        if (content2==""){
            showtitlemsg("内容有误")
        }else {
            var time = getCurrentTime();
            //pstatus=0草稿，1正常，2置顶
            $.post('publishPost.do', {
                tid: type,
                title: title,
                content: content,
                time: time,
                pstatus: 0
            }, function (back) {
                $("#title").val("");
                $("#text").val("");
                window.location.href = '/html/editor/viewPost.do?pid=' + back[0];
            });
        }
    });
}